/**
 * 
 */
package cn.code.auth.authorize.conn.app.app;

import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import io.jsonwebtoken.UnsupportedJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * @author zhailiang
 *
 */
@RestController
@RequestMapping("/userApp")
public class UserController {
	
	@Autowired
	private ProviderSignInUtils providerSignInUtils;
	
//	@Autowired
//	private AppSingUpUtils appSingUpUtils;
	
//	@Autowired
//	private SecurityProperties securityProperties;
	
	@PostMapping("/regist")
	public void regist(HttpServletRequest request) {
		
		//不管是注册用户还是绑定用户，都会拿到一个用户唯一标识。
	//	String userId = user.getUsername();
		providerSignInUtils.doPostSignUp("id", new ServletWebRequest(request));
//		appSingUpUtils.doPostSignUp(new ServletWebRequest(request), userId);
	}
	
	@GetMapping("/me")
	public Object getCurrentUser(Authentication user, HttpServletRequest request) throws ExpiredJwtException, UnsupportedJwtException, MalformedJwtException, SignatureException, IllegalArgumentException, UnsupportedEncodingException {
		
//		String token = StringUtils.substringAfter(request.getHeader("Authorization"), "bearer ");
//		
//		Claims claims = Jwts.parser().setSigningKey(securityProperties.getOauth2().getJwtSigningKey().getBytes("UTF-8"))
//					.parseClaimsJws(token).getBody();
//		
//		String company = (String) claims.get("company");
//		
//		System.out.println(company);
		
		return user;
	}

//	@PostMapping
//	@ApiOperation(value = "创建用户")
//	public User create(@Valid @RequestBody User user) {
//
//		System.out.println(user.getId());
//		System.out.println(user.getUsername());
//		System.out.println(user.getPassword());
//		System.out.println(user.getBirthday());
//
//		user.setId("1");
//		return user;
//	}

//	@PutMapping("/{id:\\d+}")
//	public User update(@Valid @RequestBody User user, BindingResult errors) {
//
//		System.out.println(user.getId());
//		System.out.println(user.getUsername());
//		System.out.println(user.getPassword());
//		System.out.println(user.getBirthday());
//
//		user.setId("1");
//		return user;
//	}

//	@DeleteMapping("/{id:\\d+}")
//	public void delete(@PathVariable String id) {
//		System.out.println(id);
//	}
//
//	@GetMapping
//	@JsonView(User.UserSimpleView.class)
//	@ApiOperation(value = "用户查询服务")
//	public List<User> query(UserQueryCondition condition,
//			@PageableDefault(page = 2, size = 17, sort = "username,asc") Pageable pageable) {
//
//		System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE));
//
//		System.out.println(pageable.getPageSize());
//		System.out.println(pageable.getPageNumber());
//		System.out.println(pageable.getSort());
//
//		List<User> users = new ArrayList<>();
//		users.add(new User());
//		users.add(new User());
//		users.add(new User());
//		return users;
//	}
//
//	@GetMapping("/{id:\\d+}")
//	@JsonView(User.UserDetailView.class)
//	public User getInfo(@ApiParam("用户id") @PathVariable String id) {
////		throw new RuntimeException("user not exist");
//		System.out.println("进入getInfo服务");
//		User user = new User();
//		user.setUsername("tom");
//		return user;
//	}

}
